<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-bav-bar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->

    <input type="file" ref="file" hidden @change="onChangeFile" />
    <!-- 个人信息 -->
    <van-cell
      class="avatar-cell"
      title="头像"
      is-link
      center
      @click="$refs.file.click()"
    >
      <van-image class="avatar" round fit="cover" :src="user.photo" />
    </van-cell>
    <van-cell
      title="昵称"
      @click="isUpdataNameShow = true"
      :value="user.name"
      is-link
    />
    <van-cell
      title="性别"
      @click="isUpdatagenderShow = true"
      :value="user.gender ? '女' : '男'"
      is-link
    />
    <van-cell
      @click="isUpdataBirthdayShow = true"
      title="生日"
      :value="user.birthday"
      is-link
    />
    <!-- /个人信息 -->

    <!-- 修改昵称 -->
    <van-popup v-model="isUpdataNameShow" position="bottom" style="height: 100%"
      ><Updataname
        v-if="isUpdataNameShow"
        @close="isUpdataNameShow = false"
        v-model="user.name"
      ></Updataname
    ></van-popup>

    <!-- 修改性别 -->
    <van-popup v-model="isUpdatagenderShow" position="bottom">
      <Updatagender
        v-if="isUpdatagenderShow"
        @close="isUpdatagenderShow = false"
        v-model="user.gender"
      ></Updatagender>
    </van-popup>

    <!-- 修改生日 -->
    <van-popup v-model="isUpdataBirthdayShow" position="bottom">
      <UpdataBirchday
        v-if="isUpdataBirthdayShow"
        v-model="user.birthday"
        @close="isUpdataBirthdayShow = false"
      ></UpdataBirchday>
    </van-popup>

    <!-- 修改头像 -->
    <van-popup
      v-model="isUpdataPhotoShow"
      position="bottom"
      style="height: 100%"
    >
      <UpdataPhoto
        @update-photo="user.photo = $event"
        @close="isUpdataPhotoShow = false"
        :img="img"
      ></UpdataPhoto>
    </van-popup>
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user'
import Updataname from './components/updata-name.vue'
import Updatagender from './components/updata-gender.vue'
import UpdataBirchday from './components/updata-birthday.vue'
import UpdataPhoto from './components/updata-photo.vue'
export default {
  name: 'UserProfile',
  components: {
    Updataname,
    Updatagender,
    UpdataBirchday,
    UpdataPhoto
  },
  props: {},
  data () {
    return {
      user: {},
      isUpdataNameShow: false, // 修改昵称弹出层显示
      isUpdatagenderShow: false, // 修改性别弹出层
      isUpdataBirthdayShow: false, // 修改生日日期弹出层
      isUpdataPhotoShow: false, // 修改头像弹出层
      img: null // 存储图片路径
    }
  },
  computed: {},
  watch: {},
  created () {
    this.loadUserProfile()
  },
  mounted () {},
  methods: {
    async loadUserProfile () {
      const res = await getUserProfile()
      console.log(res)
      this.user = res.data.data
    },

    //
    onChangeFile () {
      const file = this.$refs.file.files[0]
      this.img = window.URL.createObjectURL(file)
      this.isUpdataPhotoShow = true

      // 解决选择上次相同的图片时 不会触发弹出层
      this.$refs.file.value = ''
      console.log(this.img)
    }
  }
}
</script>

<style scoped lang="less">
.user-profile {
  .avatar-cell {
    .van-cell__value {
      display: flex;
      flex-direction: row-reverse;
    }
    .avatar {
      width: 60px;
      height: 60px;
    }
  }
  .van-popup {
    background-color: #f5f7f9;
  }
}
</style>
